package com.myway.dto.tcscenery;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DTOSceneryDetail {

	private Integer sceneryId;

	private String sceneryName;

	private Integer bookFlag;

	private String grade;

	private String address;

	private Integer cityid;

	private String city;

	private Integer provinceid;

	private String province;
	// 景点简介
	private String intro;

	private String payMode;

	private String buyNotice;

	private String lon;

	private String lat;

	private String sceneryAlias;

	private double amountAdvice;

	private Integer ifUseCard;

	public DTOSceneryDetail xmlToList(String xml) throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, DOMException,
			ParseException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Document doc = null;
		DTOSceneryDetail s = new DTOSceneryDetail();
		try {
			db = dbf.newDocumentBuilder();
			try {
				doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			NodeList list = doc.getElementsByTagName("scenery");
			NodeList nodes = doc.getElementsByTagName("city");
			Element elements = (Element)nodes.item(0);
			s.setCityid(Integer.parseInt(elements.getAttribute("id").toString()));
			NodeList nodex = doc.getElementsByTagName("province");
			Element elementx = (Element)nodex.item(0);
			s.setProvinceid(Integer.parseInt(elementx.getAttribute("id").toString()));
			for (int i = 0; i < list.getLength(); i++) {
				Element element = (Element) list.item(i);
				NodeList node = element.getChildNodes();
				Method[] methods = s.getClass().getMethods();
				for (int j = 0; j < node.getLength(); j++) {
					Node n = node.item(j);
					String attrName = n.getNodeName();
					String attrValue = "";
					if (element.getElementsByTagName(attrName).item(0)
							.getFirstChild() != null) {
						attrValue = element.getElementsByTagName(attrName)
								.item(0).getFirstChild().getNodeValue();
					}
					for (Method m : methods) {
						String methodName = m.getName();
						// 得到方法名
						if (methodName.startsWith("set")) {
							// 取出set方法 应为是要 new一个对象 然后set属性进对象
							String mName = methodName.substring(3)
									.toLowerCase();
							// 方法名取出set之后的 也就是属性名，一致转化为小写
							Class[] paramTypes = m.getParameterTypes();
							String parm = paramTypes[0].getName();
							m.setAccessible(true);// 因为写成private 所以这里必须设置
							if (mName.equals(attrName.toLowerCase())) {
								if (parm.equals("java.lang.String")) {
									m.invoke(s, attrValue); // 给属性设值
									// 这里调用set方法开始放入值
								} else if (parm.equals("double")) {
									System.out.println(attrValue);
									m.invoke(s, Double.parseDouble(attrValue));
								} else if (parm.equals("java.utils.Date")) {
									SimpleDateFormat sdf = new SimpleDateFormat(
											"yyyy-MM-dd");
									m.invoke(s, sdf.parse(attrValue));
								} else if (parm.equals("java.lang.Integer")) {
									m.invoke(s, Integer.parseInt(attrValue));
								}
							}
						}
					}

				}
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return s;
	}

	public Integer getSceneryId() {
		return sceneryId;
	}

	public void setSceneryId(Integer sceneryId) {
		this.sceneryId = sceneryId;
	}

	public String getSceneryName() {
		return sceneryName;
	}

	public void setSceneryName(String sceneryName) {
		this.sceneryName = sceneryName;
	}

	public Integer getBookFlag() {
		return bookFlag;
	}

	public void setBookFlag(Integer bookFlag) {
		this.bookFlag = bookFlag;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Integer getCityid() {
		return cityid;
	}

	public void setCityid(Integer cityid) {
		this.cityid = cityid;
	}

	
	public Integer getProvinceid() {
		return provinceid;
	}

	public void setProvinceid(Integer provinceid) {
		this.provinceid = provinceid;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getPayMode() {
		return payMode;
	}

	public void setPayMode(String payMode) {
		this.payMode = payMode;
	}

	public String getBuyNotice() {
		return buyNotice;
	}

	public void setBuyNotice(String buyNotice) {
		this.buyNotice = buyNotice;
	}

	public String getLon() {
		return lon;
	}

	public void setLon(String lon) {
		this.lon = lon;
	}

	public String getLat() {
		return lat;
	}

	public void setLat(String lat) {
		this.lat = lat;
	}

	public String getSceneryAlias() {
		return sceneryAlias;
	}


	public double getAmountAdvice() {
		return amountAdvice;
	}

	public void setAmountAdvice(double amountAdvice) {
		this.amountAdvice = amountAdvice;
	}

	public void setSceneryAlias(String sceneryAlias) {
		this.sceneryAlias = sceneryAlias;
	}


	public Integer getIfUseCard() {
		return ifUseCard;
	}

	public void setIfUseCard(Integer ifUseCard) {
		this.ifUseCard = ifUseCard;
	}
}
